class AnnotationsController < ApplicationController
   layout 'groupware'
  # GET /annotations
  # GET /annotations.xml
  def index
    @annotations = Annotation.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @annotations }
    end
  end
  
  def listByProject
    projeto = params[:id]
    @annotations = Annotation.find(:all,:conditions => ["projeto_id = ? " , projeto], :order => "created_at")
    puts @annotations
    respond_to do |format|
      format.html # listByProject.html.erb
      format.xml  { render :xml => @annotations }
    end
  end

  # GET /annotations/1
  # GET /annotations/1.xml
  def show
    @annotation = Annotation.find(params[:id])
    @projeto = Projeto.find(@annotation.projeto_id) 
    @usuario = Usuario.find(@annotation.usuario_id)

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @annotation }
    end
  end

  # GET /annotations/new
  # GET /annotations/new.xml
  def new
    @annotation = Annotation.new
    @projeto = Projeto.find(params[:id])
    @usuario = Usuario.find(session[:usuario])
    @annotation.projeto_id = params[:id]
    @annotation.projeto_id = @usuario.id
  
    
    
    respond_to do |format|
      format.html # new.tml.erb
      format.xml  { render :xml => @annotation }
    end
  end

  # GET /annotations/1/edit
  def edit
    @annotation = Annotation.find(params[:id])
      @projeto = Projeto.find(@annotation.projeto_id) 
    @usuario = Usuario.find(@annotation.usuario_id)
  end

  # POST /annotations
  # POST /annotations.xml
  def create
    puts "testeasdasd"
    @annotation = Annotation.new(params[:annotation])
    a = params[:annotation]
    @annotation.projeto_id = a["projeto_id"]
    @usuario  = Usuario.find(session[:usuario])
    puts @usuario.nome

    respond_to do |format|
      if @annotation.save
        flash[:notice] = 'Annotation was successfully created.'
        format.html { redirect_to(@annotation) }
        format.xml  { render :xml => @annotation, :status => :created, :location => @annotation }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @annotation.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /annotations/1
  # PUT /annotations/1.xml
  def update
    @annotation = Annotation.find(params[:id])

    respond_to do |format|
      if @annotation.update_attributes(params[:annotation])
        flash[:notice] = 'Annotation was successfully updated.'
        format.html { redirect_to(@annotation) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @annotation.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /annotations/1
  # DELETE /annotations/1.xml
  def destroy
    @annotation = Annotation.find(params[:id])
    @annotation.destroy

    respond_to do |format|
      format.html { redirect_to(annotations_url) }
      format.xml  { head :ok }
    end
  end
end
